package com.hui10.mediaplayer.net

import com.google.gson.Gson
import java.lang.reflect.ParameterizedType

/**
 * Author: Created by lucus on 2019/5/29.
 * E-mail: mcphilex1989@gmail.com
 * Tel: 15652608401
 * 请求的基类
 */
open class ARequest<RESPONSE>(val type: Int, val url: String, val handler: ResponseHandler<RESPONSE>) {

    private val gson by lazy {
        Gson()
    }

    /**
     * 解析返回数据
     */
    fun parseResult(result: String?): RESPONSE {
        val parameterizedType = this.javaClass.genericSuperclass as ParameterizedType
        val actualTypeArguments = parameterizedType.actualTypeArguments
        val type = actualTypeArguments[0] // 获取泛型的第一个参数的真实类型
        return gson.fromJson(result, type)
    }


    /**
     * 发送请求
     */
    fun excute() {
        NetManager.getInstance().sendRequest(this)
    }


}